<html>
<head><meta charset="utf-8"><title>env! for &amp;&#x27;static OsStr · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html">env! for &amp;&#x27;static OsStr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242970401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242970401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242970401">(Jun 17 2021 at 04:04)</a>:</h4>
<p>Is there a way to have a version of the <code>env!</code> macro that returns a <code>&amp;'static OsStr</code> rather than a <code>&amp;'static str</code>? The purpose would be to avoid breaking if a compile-time environment variable contains invalid UTF-8.</p>



<a name="242970644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242970644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242970644">(Jun 17 2021 at 04:09)</a>:</h4>
<p>There isn't right now, but it's been discussed. I'd happily support a PR adding that.</p>



<a name="242970647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242970647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242970647">(Jun 17 2021 at 04:09)</a>:</h4>
<p>I originally noticed this because everyone seems to use <code>env!("OUT_DIR")</code> to refer to outputs from a Cargo build script, which seems like it would completely break if someone has a home directory containing invalid UTF-8. To be clear, I have no particular reason to create a home directory with invalid UTF-8. But in general it seems like it's not great to have undocumented edge cases where everything suddenly breaks.</p>



<a name="242970659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242970659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242970659">(Jun 17 2021 at 04:09)</a>:</h4>
<p><code>env_os!</code>, or perhaps <code>env_path!</code> for convenience.</p>



<a name="242972921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242972921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242972921">(Jun 17 2021 at 04:57)</a>:</h4>
<p>Hrm, this seems tricky. The representation of <code>OsStr</code> isn't always going to be the same on the host and the target, but a macro like <code>env!</code> can only spit out host stuff</p>



<a name="242975629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242975629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242975629">(Jun 17 2021 at 05:49)</a>:</h4>
<p>That's true. Personally, in the case of <code>OUT_DIR</code>, I'm using <code>include_bytes!(concat!(env!("OUT_DIR"), "/foo"))</code>, so I really do want a host <code>OsStr</code> since I'm trying to resolve paths on the host. (Although thinking about it more, it seems like that won't work with a hypothetical <code>env_os!</code> anyway since <code>include_bytes!</code> and <code>concat!</code> take strings.)</p>



<a name="242978386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242978386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242978386">(Jun 17 2021 at 06:38)</a>:</h4>
<p>oh, since this is about building in non-unicode paths, IIRC that already doesn't work</p>



<a name="242978437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242978437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242978437">(Jun 17 2021 at 06:39)</a>:</h4>
<p>something about path serialization in serde? i remember digging throuhg it a while ago</p>



<a name="242978626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242978626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242978626">(Jun 17 2021 at 06:42)</a>:</h4>
<p>Given how widespread <code>env!("OUT_DIR")</code> seems to be, I'm not surprised that lots of crates wouldn't work in that scenario (although I'm not sure of the cause of the specific issue you're referring to).</p>



<a name="242978716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242978716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242978716">(Jun 17 2021 at 06:44)</a>:</h4>
<p>But ideally there would be a way to update those crates in the long term such that they wouldn't be broken for that case. (Whether it's worth the effort is still an open question.)</p>



<a name="242978889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242978889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242978889">(Jun 17 2021 at 06:47)</a>:</h4>
<p>no, sorry, what i mean is, iirc just cargo breaks if you actually try this (that is, without any code performing <code>env!("OUT_DIR")</code>). i dont remember all the details, but when i looked into it, it seemed like you needed a way to represent these paths in <code>toml</code>, which, well, toml strings need to be UTF-8.</p>



<a name="242979157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242979157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Teddy Katz <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242979157">(Jun 17 2021 at 06:50)</a>:</h4>
<p>I guess I would argue that that's a bug in cargo that would ideally be fixed (although it's very much an edge case and everyone understandably has more important things to work on).</p>



<a name="242981297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242981297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242981297">(Jun 17 2021 at 07:19)</a>:</h4>
<p>that's probably true, but it indicates the relative priority here. Also, again, these macros probably can't produce OsStr/Path actually, since those refer to target paths. so fixing this could be pretty involved — you'd need a new path type that refers to paths on the host... it's probably easier just to use strings, and to expect that while users may have cursed directory trees, at least developers can be expected to clean theirs up</p>



<a name="242995786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242995786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242995786">(Jun 17 2021 at 10:00)</a>:</h4>
<p>I was going to say that type returned probably doesn't matter all that much, as long as rustc is able to not lose information in the roundtrip.</p>



<a name="242995846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242995846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242995846">(Jun 17 2021 at 10:01)</a>:</h4>
<p>even if some new <code>env!</code> returns a byte literal, that'd be good enough, probably (and also wouldn't require wiring up the concept of OsStr as defined in libstd to the compiler implementation)</p>



<a name="242995928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/242995928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#242995928">(Jun 17 2021 at 10:02)</a>:</h4>
<p>Still needs some reasonable behaviour on Windows though. Does it produce UTF-16 bytes? wtf-8 bytes?</p>



<a name="243050269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/243050269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#243050269">(Jun 17 2021 at 17:02)</a>:</h4>
<p>The latter I think. Same as a command-line argument.</p>



<a name="243073444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/env%21%20for%20%26%27static%20OsStr/near/243073444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/env!.20for.20.26&#x27;static.20OsStr.html#243073444">(Jun 17 2021 at 19:44)</a>:</h4>
<p>i thought exposing the encoding of WTF8 was forbidden (to the point where the crate for it on <a href="http://crates.io">crates.io</a> adamantly refuses to do so as well)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>